package com.tenton.utils;

import java.nio.ByteBuffer;

/**
 * @ClassName: NumberToByteArrayUtil
 * @Description: 基本数据类型转byte数组工具类
 * @Author： tangdong
 * @Date： 2024/6/25
 */
public class NumberToByteArrayUtil {

    /**
     * int转byte数组（大端）
     *
     * @param n
     * @return
     */
    public static byte[] intToByteBig(int n) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (n & 0xff);
        bytes[2] = (byte) (n >> 8 & 0xff);
        bytes[1] = (byte) (n >> 16 & 0xff);
        bytes[0] = (byte) (n >> 24 & 0xff);
        return bytes;
    }

    /**
     * int转byte数组（小端）
     *
     * @param n
     * @return
     */
    public static byte[] intToByteLittle(int n) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >> 8 & 0xff);
        bytes[2] = (byte) (n >> 16 & 0xff);
        bytes[3] = (byte) (n >> 24 & 0xff);
        return bytes;
    }

    /**
     * short转byte数组（大端）
     *
     * @param n
     * @return
     */
    public static byte[] shortToByteBig(short n) {
        byte[] bytes = new byte[2];
        bytes[1] = (byte) (n & 0xff);
        bytes[0] = (byte) (n >> 8 & 0xff);
        return bytes;
    }

    /**
     * short转byte数组（小端）
     *
     * @param n
     * @return
     */
    public static byte[] shortToByteLittle(short n) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >> 8 & 0xff);
        return bytes;
    }


    /**
     * long转byte数组（大端）
     *
     * @param n
     * @return
     */
    public static byte[] longToByteBig(long n) {
        byte[] bytes = new byte[8];
        bytes[7] = (byte) (n & 0xff);
        bytes[6] = (byte) (n >> 8 & 0xff);
        bytes[5] = (byte) (n >> 16 & 0xff);
        bytes[4] = (byte) (n >> 24 & 0xff);
        bytes[3] = (byte) (n >> 32 & 0xff);
        bytes[2] = (byte) (n >> 40 & 0xff);
        bytes[1] = (byte) (n >> 48 & 0xff);
        bytes[0] = (byte) (n >> 56 & 0xff);
        return bytes;
    }

    /**
     * long转byte数组（小端）
     *
     * @param n
     * @return
     */
    public static byte[] longToByteLittle(long n) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >> 8 & 0xff);
        bytes[2] = (byte) (n >> 16 & 0xff);
        bytes[3] = (byte) (n >> 24 & 0xff);
        bytes[4] = (byte) (n >> 32 & 0xff);
        bytes[5] = (byte) (n >> 40 & 0xff);
        bytes[6] = (byte) (n >> 48 & 0xff);
        bytes[7] = (byte) (n >> 56 & 0xff);
        return bytes;
    }

    /**
     * byte数组转int(大端)
     *
     * @param data
     * @return
     */
    public static int byteToIntBig(byte[] data) {
        int int1 = data[3] & 0xff;
        int int2 = (data[2] & 0xff) << 8;
        int int3 = (data[1] & 0xff) << 16;
        int int4 = (data[0] & 0xff) << 24;
        return int1 | int2 | int3 | int4;
    }

    /**
     * byte数组转int(小端)
     *
     * @param data
     * @return
     */
    public static int byteToIntLittle(byte[] data) {
        int int1 = data[0] & 0xff;
        int int2 = (data[1] & 0xff) << 8;
        int int3 = (data[2] & 0xff) << 16;
        int int4 = (data[3] & 0xff) << 24;
        return int1 | int2 | int3 | int4;
    }

    /**
     * byte转short(大端)
     *
     * @param data
     * @return
     */
    public static short byteToShortBig(byte[] data) {
        return (short) (data[0] << 8 | data[1] & 0xff);
    }

    /**
     * byte转short（小端）
     *
     * @param data
     * @return
     */
    public static short byteToShortLittle(byte[] data) {
        return (short) (data[1] << 8 | data[0] & 0xff);
    }


    /**
     * byte数组转long(大端)
     *
     * @param data
     * @return
     */
    public static long byteToLongBig(byte[] data) {
        long long1 = (long) (data[7] & 0xff);
        long long2 = (long) (data[6] & 0xff) << 8;
        long long3 = (long) (data[5] & 0xff) << 16;
        long long4 = (long) (data[4] & 0xff) << 24;
        long long5 = (long) (data[3] & 0xff) << 32;
        long long6 = (long) (data[2] & 0xff) << 40;
        long long7 = (long) (data[1] & 0xff) << 48;
        long long8 = (long) (data[0] & 0xff) << 56;
        return long1 | long2 | long3 | long4 | long5 | long6 | long7 | long8;
    }

    /**
     * byte数组转long(大端)
     *
     * @param data
     * @return
     */
    public static long byteToLongLittle(byte[] data) {
        long long1 = (long) (data[7] & 0xff) << 56;
        long long2 = (long) (data[6] & 0xff) << 48;
        long long3 = (long) (data[5] & 0xff) << 40;
        long long4 = (long) (data[4] & 0xff) << 32;
        long long5 = (long) (data[3] & 0xff) << 24;
        long long6 = (long) (data[2] & 0xff) << 16;
        long long7 = (long) (data[1] & 0xff) << 8;
        long long8 = (long) (data[0] & 0xff);
        return long1 | long2 | long3 | long4 | long5 | long6 | long7 | long8;
    }

    /**
     * byte转float
     *
     * @param bytes
     * @return
     */
    public static float byteToFloat(byte[] bytes) {
        ByteBuffer wrap = ByteBuffer.wrap(bytes);
        return wrap.getFloat();
    }

    /**
     * byte转double
     *
     * @param bytes
     * @return
     */
    public static double byteToDouble(byte[] bytes) {
        ByteBuffer wrap = ByteBuffer.wrap(bytes);
        return wrap.getDouble();
    }

    /**
     * 数组截取
     *
     * @param source     原数组
     * @param startIndex 开始截取下标
     * @param length     截取长度
     * @return 截取后的数据
     */
    public static byte[] byteIntercept(byte[] source, Integer startIndex, Integer length) {
        byte[] bytes = new byte[length];
        System.arraycopy(source, startIndex, bytes, 0, length);
        return bytes;
    }

    /**
     * 数组合并
     *
     * @param bytes1 数组1 放在合并数组前面
     * @param bytes2 数组2 放在合并数组后面
     * @return 合并数组
     */
    public static byte[] byteMerge(byte[] bytes1, byte[] bytes2) {
        //创建一个新的byte数组，长度为两个数组的长度之和
        byte[] mergedBytes = new byte[bytes1.length + bytes2.length];
        //将bytes1的元素复制到新数组中
        System.arraycopy(bytes1, 0, mergedBytes, 0, bytes1.length);
        //将bytes2的元素复制到新数组中
        System.arraycopy(bytes2, 0, mergedBytes, bytes1.length, bytes2.length);
        return mergedBytes;
    }
}
